function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('.') + ' ' + [hour, minute].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function formatDate(date, split) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  split = split || '.'

  return [year, month, day].map(formatNumber).join(split)
}

function showErrorToast(msg) {
  wx.showToast({
    title: msg,
    image: '/static/images/icon_error.png'
  })
}

function getDaysAfterToday(n) {
  var now = new Date()
  var nowTime = now.getTime()
  var oneDayTime = 24 * 60 * 60 * 1000
  var days = []
  for (var i = 0; i < n; i++) {
    // 显示周一
    var ShowTime = nowTime + i * oneDayTime
    // 初始化日期时间
    var myDate = new Date(ShowTime)
    var year = myDate.getFullYear()
    var month = myDate.getMonth() + 1
    var date = myDate.getDate()
    var str = '星期' + '日一二三四五六'.charAt(myDate.getDay())
    days.push({
      date: year + '-' + month + '-' + date,
      week: str
    })
  }

  return days
}

module.exports = {
  formatTime,
  showErrorToast,
  formatDate,
  getDaysAfterToday
}